<template>
	<view>
		<div class="qrCode">
			<!-- <canvas id="qrcode" canvas-id="qrcode" style="width: 400px;height: 400px;"></canvas> -->
			<uv-qrcode style="margin: auto;" size="250" ref="qrcode" :value="qrCodeText"></uv-qrcode>

			<div>请使用微信扫描上方二维码支付订单</div>
			<div>支付金额：{{amount}}</div>
			<div class="cancel" @click="cancel()">取消支付</div>

		</div>
	</view>
</template>

<script lang="ts">
	import { globalVariable } from '../../globalVariable/globalVariable';
	import { uiCollection } from '../../Providing/uiCollection';
	import { request } from '../../Providing/request'

	const printPlugin = uni.requireNativePlugin('regoPrint');

	export default {
		data() {
			return {
				qrCodeText: globalVariable.qrCodePay.url,
				payType: globalVariable.qrCodePay.type,
				amount: globalVariable.qrCodePay.amount,
				poll: null,
			};
		},
		methods: {
			cancel() {
				uiCollection.showModal("确定要取消支付吗，取消后将回到登录页").then(res => {
					if (res) {
						clearInterval(this.poll);
						this.poll = null;
						uni.$emit('goToHome');
					}
				})
			},

			pollPayStatus() {
				this.poll = setInterval(() => {
					uiCollection.showLoading("支付结果查询中")
					new request().getRegister(globalVariable.qrCodePay.payId).then((res) => {
						console.log("支付结果查询返回" + JSON.stringify(res))
						if (res['status'] == 2) {
							uiCollection.hideLoading();
							clearInterval(this.poll);
							this.poll = null;

							// uiCollection.showToast("支付成功,即将返回登录页");

							uiCollection.showLoading("支付成功，执行小票打印");
							let printArray = [{
								type: "text",
								msg: "姓名：" + globalVariable.realName
							}, {
								type: "text",
								msg: "挂号科室：" + String(res['depName'])
							}, {
								type: "text",
								msg: "挂号类型：" + String(res['regType'])
							}, {
								type: "text",
								msg: "号序：" + String(res['registerHx']) + "号"
							}, {
								type: "text",
								msg: "挂号单号：" + String(res['hisGhdh'])
							}, {
								type: "text",
								msg: "挂号费用：" + String(res['registerPrice'])
							}, {
								type: "text",
								msg: "创建时间：" + String(uiCollection.TimeDate(new Date(parseInt(res['createTime']) * 1000)))
							}, {
								type: "text",
								msg: "预约时间：" + String(uiCollection.TimeDate(new Date(parseInt(res['registerTime']) * 1000), "noSecond"))
							}, {
								type: "text",
								msg: "支付时间：" + String(uiCollection.TimeDate(new Date(parseInt(res['payTime']) * 1000)))
							}]
							printPlugin.print(JSON.stringify(printArray), (res) => {
								uiCollection.hideLoading()
								console.log("插件返回内容", res)
								// uni.showToast({
								// 	title:this.msg
								// })
							})

							uni.$emit("goToHome");
						}
					})
				}, 3000)
			},

			getPayList() {
				this.poll = setInterval(() => {
					new request().postPaymentNote().then((res : any) => {
						if (res.length > 0) {
							let arr=res.filter(item => item['ZFZT'] !== '1');
							if (arr.length == 0) {
								clearInterval(this.poll);
								this.poll = null;
								uiCollection.showToast("缴费成功");
								uni.$emit("goToHome");
							}
						}
					}).catch((err) => {
						uiCollection.showToast(err)
					})
				}, 3000)

			}
		},
		created() {
			this.poll = null;
			if (globalVariable.qrCodePay.type == "挂号") {
				this.pollPayStatus()
			}

			if (globalVariable.qrCodePay.type == "缴费") {
				this.getPayList()
			}
		},
		onLoad(option) {
			//home页面发起登录后检测到需注册健康卡，传入发起登录身份证号
			// this.qrCodeText = option.qrCodeText;
			// this.payType = option.payType;

		},
	}
</script>

<style lang="scss">
	.qrCode {
		margin: 50px;
		text-align: center;

		div {
			height: 30px;
			text-align: 50px;
			line-height: 50px;
		}

		.cancel {
			width: 200px;
			height: 40px;
			line-height: 40px;
			text-align: center;
			border: 2px solid #555555;
			border-radius: 20px;
			margin: 30px auto;
		}
	}
</style>